﻿//************************************************************************************************
// Copyright © 2012 Steven M. Cohn. All Rights Reserved.
//
//************************************************************************************************ 

namespace CookieChipper
{
	using System;
	using System.Collections.Generic;
	using System.Windows.Forms;


	internal partial class BackupRestoreWizard : OptionalForm
	{
		private WizardPage page;


		public enum Results
		{
			Cancel,
			Backup,
			Restore
		}


		//========================================================================================
		// Lifecycle
		//========================================================================================

		public BackupRestoreWizard ()
		{
			InitializeComponent();

			page = GetPage<StartPage>("StartPage", true);
			Result = Results.Cancel;
		}

	
		private void DoClosing (object sender, FormClosingEventArgs e)
		{
			foreach (Control control in contentPanel.Controls)
			{
				control.Dispose();
			}

			page = null;
		}


		/// <summary>
		/// Gets the result of this backup/restore wizard.
		/// </summary>

		public Results Result
		{
			get;
			private set;
		}


		//========================================================================================
		// Methods
		//========================================================================================

		private WizardPage GetPage<T> (string name, bool forward) where T : class, new()
		{
			if (page != null)
			{
				//if (!page.OnLeavePage(forward))
				//{
				//	return page;
				//}

				page.Visible = false;
			}

			if (contentPanel.Controls.ContainsKey(name))
			{
				page = contentPanel.Controls[name] as WizardPage;
				page.Visible = true;
			}
			else
			{
				page = new T() as WizardPage;
				page.Dock = DockStyle.Fill;
				page.StateChanged += DoStateChanged;
				contentPanel.Controls.Add(page);
			}

			titleLabel.Text = page.Description;
			//page.OnEnterPage(forward);
			page.Focus();
			return page;
		}
		

		private void DoNextPage (object sender, EventArgs e)
		{
			if (page.Name.Equals("StartPage"))
			{
				if (((StartPage)page).IsBackup)
				{
					page = GetPage<BackupPage>("BackupPage", true);
				}
				else
				{
					page = GetPage<RestorePage>("RestorePage", true);
				}
			}
			else if (page.Name.Equals("BackupPage") || page.Name.Equals("RestorePage"))
			{
				if (page.Execute())
				{
					bool isBackup = page.Name.Equals("BackupPage");
					string info = page.Information;

					page = GetPage<CompletePage>("CompletePage", true);

					if (page.Name.Equals("CompletePage"))
					{
						((CompletePage)page).IsBackup = isBackup;
						page.Information = info;
					}

					Result = isBackup ? Results.Backup : Results.Restore;
				}
				else
				{
					MessageBox.Show(page.Information, "Not completed",
						MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			else
			{
				this.Close();
			}
		}


		private void DoPreviousPage (object sender, EventArgs e)
		{
			page = GetPage<StartPage>("StartPage", false);
		}


		private void DoStateChanged (WizardPage page, PageStates pageState)
		{
			prevButton.Enabled = !page.Name.Equals("StartPage");

			nextButton.Enabled =
				(pageState == PageStates.Ready) || (pageState == PageStates.Complete);

			if (page.Name.Equals("CompletePage"))
			{
				prevButton.Visible = false;
				nextButton.Text = "OK";
			}
		}
	}
}
